package com.fangque.kepp.framework.exceptionhandle;

import com.fangque.kepp.framework.result.ResultBean;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.json.MappingJackson2JsonView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author liyk
 */
@ControllerAdvice
public class CustomExceptionResolver {

    @ExceptionHandler(Exception.class)
    @ResponseBody
    public ResultBean resolveException(HttpServletRequest httpServletRequest
            , HttpServletResponse response, Object o, Exception e) {
        ModelAndView mv = new ModelAndView(new MappingJackson2JsonView());
        String errorMsg = "操作失败, 原因: "+ e.getLocalizedMessage() + "!";

        return ResultBean.error(errorMsg);
    }
}